package com.splant.android.smart.SystemDefinedLevel.SystemControls.SetValuePopupWindow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.SeekBar;

import com.splant.android.R;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Dimen_Util;

/**
 * Created by Administrator on 2016/10/5.
 */
public class SetValuePopupWindow extends PopupWindow implements View.OnClickListener {

    private LayoutInflater inflater;
    private ImageButton imgBtn_close;
    private ImageButton imgBtn_reduce;
    private ImageButton imgBtn_add;
    private SeekBar skBar_progress;
    private int maxValue = 100;
    private int minValue = 0;
    private int progress;

    private Context mContext;
    public SetValuePopupWindow(Context context, IValueChanged iValueChanged) {

        super(context);
        mContext=context;
        inflater = LayoutInflater.from(context);
        init();
        this.iValueChanged = iValueChanged;
    }

    private void init() {
        View view = inflater.inflate(R.layout.system_setvalue_module, null);
        setContentView(view);

        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.system_btn_del);
        int h=bitmap.getHeight();
        int contextHeight= System_Dimen_Util.dip2px(mContext,50);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(h+contextHeight);

        imgBtn_reduce = (ImageButton)view.findViewById(R.id.reduceValue);
        imgBtn_add = (ImageButton)view.findViewById(R.id.addValue);
        skBar_progress =(SeekBar)view.findViewById(R.id.skBar_progress);
        imgBtn_close= (ImageButton)view.findViewById(R.id.imgBtn_close);
        imgBtn_close.setVisibility(View.VISIBLE);
        skBar_progress.setMax(100);

        imgBtn_reduce.setOnClickListener(this);
        imgBtn_add.setOnClickListener(this);
        imgBtn_close.setOnClickListener(this);
        skBar_progress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (isInit==false)
                {
                    progress = i + minValue;
                    iValueChanged.valueChanged(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

                isInit=false;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
    private boolean isInit=false;
    public void setRange(int minValue, int maxValue) {
        if (minValue < maxValue) {
            isInit=true;
            this.minValue = minValue;
            this.maxValue = maxValue;
            skBar_progress.setMax(maxValue - minValue);
        }
    }

    public void setProgress(int v) {
        if (v < minValue)
           v=minValue;
        if (v>maxValue)
            v=maxValue;
        progress = v;
        skBar_progress.setProgress(progress - minValue);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reduceValue:
                setProgress(progress--);
                iValueChanged.valueChanged(progress);
                break;
            case R.id.addValue:
                setProgress(progress++);
                iValueChanged.valueChanged(progress);
                break;
            case R.id.imgBtn_close:
                dismiss();
                iValueChanged.setFinish();
                break;
        }
    }
    public IValueChanged iValueChanged;
    public interface IValueChanged {
        void valueChanged(int value);
        void setFinish();
    }
}
